<template>
  <div class="SignFlow">
    <div class="SignFlow__tabs">
      <div class="SignFlow__tabs--title">LOG IN</div>
      <div class="SignFlow__tabs--switch" @click="changeSignType">
        <div class="toolTip-right">
          密码登录
        </div>
        <i class="iconfont iconhalf_pc"></i>
      </div>
    </div>
    <div class="SignFlow__QrCode ScanQrCode__container">
      <ScanQrCode :redirectUrl="`/login?type=SignInByQrCode`" scenes="AccountLoggin"/>
    </div>
  </div>
</template>

<script lang="ts">
import {Vue, Component, Emit} from "vue-property-decorator";
import ScanQrCode from "@/components/ScanQrCode.vue";
import {unionAccountLogin} from "@/api/sign";
import {login, modifySignStatus} from "@/libs";
import storage from "@/libs/storage";

@Component({
  components: {
    ScanQrCode,
  },
})
export default class SignIn extends Vue {
  created() {
    if (this.$route.query.code && this.$route.query.type === "SignInByQrCode") {
      const cd = this.$route.query.code;
      history.pushState({}, "", "/login");
      this.submitForm(cd);
    }
  }

  async submitForm(cd) {
    try {
      const form = {
        loginType: 3,
        certificate: "",
        phone: "",
        password: "",
        code: cd,
      };
      const response = await unionAccountLogin(form);
      const {data, code} = response.data;
      if (code === 200) {
        modifySignStatus(data.token, data);
        login(data.token, data);
        if (data.shopInfoVo && data.shopInfoVo.backUrl && storage.get("entryType") === "back") {
          // 删除官网目标网址信息
          storage.delete("entryType");
          // 生产环境
          open(`${data.shopInfoVo.backUrl}`, "_top");
        } else {
          await this.$router.push(process.env.VUE_APP_PUBLICPATH);
        }
      }
    } catch (e) {
      this.$message({
        type: "warning",
        message: e,
      });
    }
  }

  @Emit("change")
  changeSignType() {
    return "SignInByPhone";
  }
}
</script>

